import subprocess

PLAY_VIDEO_POSITION = (532, 662)

NEXT_VIDEO_HEIGHT = 754
SWIPE_DURATION_IN_MS = 500

RESULT_CLOSE_POSITION = (854, 560)
JUMP_ANIMATION_DURATION_IN_SECONDS = 6
RESULT_ANIMATION_DURATION_IN_SECONDS = 1


def connect_phone():
    return True
    result = subprocess.check_output("adb devices")
    print(result)
    return " device " in result


def touch_screen(x, y):
    subprocess.Popen("adb shell input  touchscreen tap {x} {y}".format(x=x, y=y)).wait()


def next_video():
    subprocess.Popen("adb shell input swipe {} {} {} {} {}".format(PLAY_VIDEO_POSITION[0],
                                                                   (PLAY_VIDEO_POSITION[1]) + NEXT_VIDEO_HEIGHT,
                                                                   (PLAY_VIDEO_POSITION[0]),
                                                                   (PLAY_VIDEO_POSITION[1]),
                                                                   SWIPE_DURATION_IN_MS)).wait()


if __name__ == "__main__":
    if not connect_phone():
        print("Connect phone failed")

    for i in range(10):
        print(u"{} 次播放".format(i + 1))
        next_video()
        continue
        # 按一下起跳
        touch_screen(PLAY_VIDEO_POSITION[0], PLAY_VIDEO_POSITION[1])

        # print("等待 {} 起跳动画结束".format(JUMP_ANIMATION_DURATION_IN_SECONDS))
        # # 等待起跳动画完结，显示结果
        time.sleep(150)

        next_video()
        #
        # print("关闭结果，再次起跳准备")
        # # 关闭结果
        # touch_screen(RESULT_CLOSE_POSITION[0],RESULT_CLOSE_POSITION[1])
        #
        # # 等待起跳画面再次就绪
        # print("等待 {} 结果动画结束".format(RESULT_ANIMATION_DURATION_IN_SECONDS))
        # time.sleep(RESULT_ANIMATION_DURATION_IN_SECONDS)
